import { getToken, setToken } from '@/utils/auth.js'
import { userProfile } from '@/api/user.js'

const state = {
  token: getToken() || '',
  userProfile: '',
  refresh_token: localStorage.getItem('refresh_token') || ''
}
const mutations = {
  setToken (state, value) {
    state.token = value
    setToken(value)
  },
  setUserProfile (state, value) {
    state.userProfile = value
  },
  editUserProfile (state, value) {
    state.userProfile[value[0]] = value[1]
  },
  setRefreshToken (state, value) {
    state.refresh_token = value
    localStorage.setItem('refresh_token', value)
  }
}
const actions = {
  async getUserProfile (store) {
    if (store.state.userProfile === '') {
      const res = await userProfile()
      store.commit('setUserProfile', res.data)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
